package com.glacier.yuyuan.modules.admin.sys.web;

import cn.hutool.core.util.StrUtil;
import com.glacier.common.core.domain.Page;
import com.glacier.common.core.domain.Result;
import com.glacier.common.core.domain.form.ListWrapper;
import com.glacier.common.core.factory.TreeFactory;
import com.glacier.yuyuan.common.web.BaseController;
import com.glacier.yuyuan.modules.admin.sys.domain.Area;
import com.glacier.yuyuan.modules.admin.sys.service.AreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * date 2023-01-06 10:50
 * 区域控制层
 *
 * @author glacier
 * @version 1.0
 */
@RequestMapping("${yuyuan.adminPath}/sys/area")
@Controller
public class AreaController extends BaseController {
    private AreaService areaService;

    @Autowired
    public void setAreaService(AreaService areaService) {
        this.areaService = areaService;
    }

    @GetMapping("")
    public String list() {
        return "admin/sys/area/areaList";
    }

    @ResponseBody
    @PostMapping("/data")
    public Result<List<Area>> data(Area area) {
        List<Area> list = areaService.findList(area);
        return Result.ok("ok", list, list.size());
    }

    @ResponseBody
    @PostMapping("/page")
    public Result<List<Area>> page(Page<Area> page, Area area) {
        page = areaService.findPage(page, area);
        return Result.ok("ok", page.getData(), page.getTotal());
    }

    @ResponseBody
    @GetMapping("/tree")
    public List<Area> tree(Area area) {
        return TreeFactory.buildTree(areaService.findList(area));
    }

    @GetMapping("/form")
    public String form(Area area, Model model) {
        if (StrUtil.isNotBlank(area.getId())) {
            area = areaService.findById(area.getId());
        }
        model.addAttribute("entity", area);
        return "admin/sys/area/areaForm";
    }

    @PostMapping("/save")
    @ResponseBody
    public Result<String> save(Area area) {
        areaService.save(area);
        return Result.ok("操作成功！", "");
    }

    @ResponseBody
    @PostMapping("/del")
    public Result<String> del(ListWrapper<String> form) {
        areaService.batchDelete(form.getDatas());
        return Result.ok("操作成功", "");
    }
}
